/*
*dialog插件
*/     
(function($) {     
 	$.extend({
		dialog:function(options){
			//默认配置
			var defaults = {
                container:'body',
				//对话框ID
				id: false,		
			    //消息内容
			    content: "弹出框内容区",
			    //确定按钮文本
			    okValue: '确定',
			    //取消按钮文本
			    cancelValue: '取消', 
			    //标题
			    title: "Viki友情提示",
			    //内容宽度
			    width: "auto",
			    //内容高度
			    height: "auto",
			    //是否锁屏
			    lock: true,
                //遮罩背景透明度
                maskOpacity:'0.6',
				//是否拖动
				drag:true,
                //是否只是水平方向上可以拖动，ps:前提设置drag为true
                dragOnlyX:false,
                //是否只是竖直方向上可以拖动，ps:前提设置drag为true
                dragOnlyY:false,
				//是否自动关闭
				autoclose:false,
                //自动关闭时的回调函数
                autoCloseCallback:function(){},
                //最小宽度
                minWidth:200,
                //是否显示关闭按钮
                showClose:true,
                //是否显示title
                showTitle:true,
                //主体内容的垂直对齐方式 top(顶部对齐) | middle(中部对齐) | bottom(下部对齐)
                contentValign:'middle',
                //left(左对齐) | center(居中对齐) | right(右对齐) | justify(两端对齐)
                contentAlign:'left',
                //内容的背景颜色
                contentBackground:'#FFFFFF',
                //dialog框的偏移量
                offset:{left:0,top:0},
                //设置全局的前景颜色
                globalBackground:'#0099CC',
                //设置全局的文字颜色
                globalColor:'#ffffff',
                //设置是否弹出框使用圆角，使用写圆角半径，不使用写false;
                globalRadius:'0px',
                //设置弹出框的边框样式
                globalBorder:'1px solid #336699',
                //设置弹出框的内容内填充
                contentPadding:'20px 25px',
                //设置是否点击背景遮罩时关闭弹出框
                maskClickClose:false,
                //是否使用相对窗口定位
                fixed:false,
                //是否显示边框背影
                showBorderShadow:true,
                //自动打开
                autoOpen:true
//                fixedposition:{horizontal:'center', vertical:'middle'}
			}; 
			this.config = $.extend(defaults, options);
		    this.config.id = this.config.id ? this.config.id : new Date().getTime();
		    //插入DOM
		    this.dom = $('<div id="' + this.config.id + '" class="jq_dialog_div">' + $.dialog.templates + '</div>').hide();
		    this.dom.css({'position': 'absolute', 'z-index': '9999'});
	    	$(this.config.container).append(this.dom);
			/**
		     * 初始化设置
		     */
		    this.init = function () {	
		    	/*this.dom = dom;
		    	this.config = config;*/
                this.dom.find('.openDialog_buttons').hide();
				//设置内容	
				this.content(this.config.content,this.config.contentAlign,this.config.contentValign);
				//设置标题
                if(this.config.showTitle)
				    this.title(this.config.title);
                else
                    this.dom.find('.openDialog_header').hide();
				//设置尺寸
				this.size(this.config.width, this.config.height, this.config.minWidth);
				//设置居中
				this.position(this.config.offset,this.config.container);
				//设置锁屏
				if (this.config.lock) {
					this.lock();
                    if(this.config.backClickClose){
                        var $this = this;
                        $('#mask_'+this.config.id).click(function(){
                            $this.close.call($this);
                        })
                    }
                    $('.openDialog_mask').css({opacity:this.config.maskOpacity,filter: 'alpha(opacity='+this.config.maskOpacity*100+')'})
				}
				if(this.config.drag)
				{
					//拖拽支持
					var mouse = {x:0, y:0};
                    var $this = this;
					function drag(event) {
		            	var e = window.event || event;
		            	var top = parseInt($this.dom.css('top')) + (e.clientY - mouse.y);
		            	var left = parseInt($this.dom.css('left')) + (e.clientX - mouse.x);
//                        $this.dom.css({top:top,left:left});
                        if($this.config.dragOnlyX == true){
                            mouse.x = e.clientX;
                            $this.dom.css({left:left});
                        }else if($this.config.dragOnlyY){
                            mouse.y = e.clientY;
                            $this.dom.css({top:top});
                        }else{
                            mouse.x = e.clientX;
                            mouse.y = e.clientY;
                            $this.dom.css({top:top,left:left});
                        }
		        	}
                    this.dom.find(".openDialog_header").mousedown(function(event){
						var e = window.event || event;
                        if($this.config.dragOnlyX){

                        }else if($this.config.dragOnlyX){
                            mouse.y = e.clientY;
                        }else{
                            mouse.x = e.clientX;
                            mouse.y = e.clientY;
                        }
                        $(document).bind('mousemove', drag);
					});
					$(document).mouseup(function(event){
		            	$(document).unbind('mousemove', drag);
		        	});
				}
                if(this.config.showClose){
                    //监听事件
                    var _this = this;
                    this.dom.find(".openDialog_close").bind("click",function(){_this.close.call(_this);});
                }else{
                    this.dom.find('.openDialog_close').remove();
                }
				//自动关闭窗口
				var thisobj = this;
				if(this.config.autoclose){
					setTimeout(function(){thisobj.close(thisobj);thisobj.config.autoCloseCallback.call();}, this.config.autoclose);
				}
                //是否设置相对定位
                if(this.config.fixed){
                    this.keepfix(this.config.fixedposition,this.config.vertical);
                }
                if(this.config.showBorderShadow){
                    this.dom.find('.openDialog').css('boxShadow','0 3px 26px rgba(0, 0, 0, .9)');
                }
			}
			
			/**
			 * 打开对话框
			 */
			this.open = function () {
				this.init();
                if(this.config.autoOpen)
                    this.dom.fadeIn();
			}
		    //设置背景颜色及填充
            this.dom.find('.openDialog_main').css({background:this.config.contentBackground,padding:this.config.contentPadding});
            //设置全局背景颜色
            this.dom.find('.openDialog_header').css('backgroundColor',this.config.globalBackground);
            //设置全局字体颜色
            this.dom.find('.openDialog_header,a.openDialog_close,.openDialog_buttons .button').css('color',this.config.globalColor);
            //设置dialog边框颜色
            this.dom.find('.openDialog').css({border:this.config.globalBorder,borderRadius:this.config.globalRadius});
			/**
		     * 设置内容
		     * @param {String} 内容
		     */
			this.content = function (message,align,valign) {
				if (typeof message === "string") {
                    this.dom.find(".openDialog_main").html(message);
		        }else{
                    this.dom.find('.openDialog_main').append(message);
                }
                this.dom.find('.openDialog_main').attr('align',align);
                this.dom.find('.openDialog_main').attr('valign',valign);
			}

			/**
		     * 设置标题
		     * @param {String, Boolean}	标题内容. 为 false 则隐藏标题栏
		     */
			this.title = function (content) {
		        if (content === false) {
                    this.dom.find(".openDialog_header").hide();
                    this.dom.find(".openDialog_title").html('')
		        } else {
                    this.dom.find(".openDialog_header").show();
                    this.dom.find(".openDialog_title").html(content)
		        };
		    }
			
			/**
			 * 位置居中
			 */
			this.position = function (offset,container) {
                if(container == 'body'){
				var left = ($(window).width() - this.dom.outerWidth()) / 2.0;
	            var top = ($(window).height() - this.dom.outerHeight()) /3.0;
                top = top > 0 ? top :0;
                this.dom.css({
	            	top: top + $(document).scrollTop()+offset.top,
	            	left: left + $(document).scrollLeft()+offset.left
	            });
                }else{
                    this.dom.css({
                        top: offset.top,
                        left: offset.left
                    });
                }
			}
			
			/**
		     * 尺寸
		     * @param {Number, String} 宽度
		     * @param {Number, String} 高度
		     */
			this.size = function (width, height, minWidth) {
				if (typeof width === "number") {
		            width = width + "px";
		        };
		        if (typeof height === "number") {
		            height = height + "px";
		        };
                if (typeof minWidth == 'number'){
                    minWidth = minWidth + 'px';
                }
                this.dom.find(".openDialog_main").css("width", width);
                this.dom.find(".openDialog_main").css("height", height);
                this.dom.find(".openDialog_main").css("min-width", minWidth);
		    }
			
			/**
		     * 显示对话框
		     */
			this.visible = function () {
                this.dom.css("visibility", "visible");
		    }
			
			/**
		     * 隐藏对话框
		     */
			this.hide = function () {
                this.dom.fadeOut("slow", function(){
                    $(this).hide();
                });
                $('#mask_'+this.config.id).hide();
		    }

            /**
             * 显示对话框
             */
            this.show = function(){
                this.dom.fadeIn("fast", function(){
                    $(this).show();
                });
                $('#mask_'+this.config.id).show();
            }
			
			/**
		     * 关闭对话框
		     */
			this.close = function () {
                this.dom.fadeOut("slow", function(){
		        	$(this).remove();
		        });
		        $('#mask_'+this.config.id).remove();
		    }
			
			/**
		     * 锁屏
		     */
			this.lock=function(){	
				$("body").append('<div id="mask_' + this.config.id + '" class="openDialog_mask"></div>');
				$(".openDialog_mask").css({
					'z-index': '8888',
        			"left":"0px",
        			"top":"0px",
        			"width":$(document).width()+'px',   //"100%"
        			"height":$(document).height()+'px', //"100%"
					'display': 'block'
				});				
			};
            /**
             * 保持相对位置
             */
            this.keepfix = function(horizontal, vertical) {
                var this_obj = this;
                if (navigator.userAgent.indexOf("MSIE 6.0") != -1){
                    $(window).scroll(function(){
                        this_obj.position({left:0,top:0});
                    });
                }else{
                    this.dom.css({position:'fixed'});
                }
            };

            this.update = function(option){
                if(option.okValue){
                    this.dom.find('.ok').html(option.okValue)
                }
            }
		}

	});
	
	/**
	 * HTML模板
	 */
	$.dialog.templates = 
	'<div class="openDialog">'
	+	'<table class="openDialog_border" border="0" cellspacing="0" cellpadding="0">'
	+		'<tbody>'
	+			'<tr>'
	+				'<td class="openDialog_header openDialog_td">'
	+					'<div class="openDialog_title" >Title</div>'
	+					'<a class="openDialog_close" href="javascript:" style="text-decoration:none;">×</a>'
	+				'</td>'
	+			'</tr>'
	+			'<tr>'
	+				'<td class="openDialog_main openDialog_td">'
	+				'</td>'
	+			'</tr>'
	+			'<tr>'
	+				'<td class="openDialog_footer openDialog_td">'
	+					'<div class="openDialog_buttons"></div>'
	+				'</td>'
	+			'</tr>'
	+		'</tbody>'
	+	'</table>'
	+'</div>';	
	
	/*窗口对象展现方法集合*/
	$.dialog.open = function(options){
		var dialog = new $.dialog(options);
		dialog.init();	 
		dialog.dom.fadeIn();   
		return dialog;
	};
	
	$.dialog.alert = function(content, opt, callback){
		var defaultopt = {
				lock: true,
				content: content
			}
		var myopt = $.extend(defaultopt, opt);
		var dialog = new $.dialog(myopt);
        dialog.init();
        if(dialog.config.okValue){
            $('#'+dialog.config.id).find('.openDialog_buttons').html('<span class=" button ok" >'+dialog.config.okValue+'</span>');
            dialog.dom.find('.openDialog_buttons').show();
            callback ? $('#'+dialog.config.id).find('.ok').bind('click',callback):void(0);
            $('#'+dialog.config.id).find('.ok').bind('click',function(){dialog.close.call(dialog);});
        }else{
            $('#'+dialog.config.id).find('.openDialog_footer').remove();
        }
        dialog.dom.find('.openDialog_buttons .button').css({backgroundColor:dialog.config.globalBackground,color:dialog.config.globalColor,borderRadius:dialog.config.globalRadius,border:dialog.config.globalBorder});
		dialog.dom.fadeIn();

		return dialog;
	};
	$.dialog.confirm = function(opt, callbackok, callbackcancel){
		var dialog = new $.dialog(opt);
        dialog.init();
        dialog.dom.find('.openDialog_buttons').append('<span class="button ok" >'+dialog.config.okValue+'</span>');
        dialog.dom.find('.openDialog_buttons').append('<span class="button cancel" >'+dialog.config.cancelValue+'</span>');
//        dialog.dom.find('.openDialog_buttons .button').css({backgroundColor:dialog.config.globalBackground,color:dialog.config.globalColor,borderRadius:dialog.config.globalRadius,border:dialog.config.globalBorder});
		dialog.dom.find('.openDialog_buttons').show();
        dialog.dom.find('.openDialog_buttons .button').css({backgroundColor:dialog.config.globalBackground,color:dialog.config.globalColor,borderRadius:dialog.config.globalRadius,border:dialog.config.globalBorder});
		dialog.dom.fadeIn(); 
		callbackcancel ? dialog.dom.find('.cancel').bind('click',callbackcancel):void(0);
		dialog.dom.find('.cancel').bind('click',function(){dialog.close.call(dialog);});
		callbackok ? dialog.dom.find('.ok').bind('click',callbackok):void(0);
		dialog.dom.find('.ok').bind('click',function(){dialog.close.call(dialog);});
	};
//        $.dialog.show({width:500,height:300,title:'mydialogtitle',okValue:'确定',cancelValue:'取消',content:$('.titletxt')});
    $.dialog.show = function(opt, callbackok, callbackcancel){
        var dialog = new $.dialog(opt);
        dialog.init();
        if(callbackok){
            dialog.dom.find('.openDialog_buttons').append('<span class="button ok">' + dialog.config.okValue + '</span>');
            dialog.dom.find('.ok').bind('click',callbackok);
        };
        if(callbackcancel){
            dialog.dom.find('.openDialog_buttons').append('<span class="button cancel">' + dialog.config.cancelValue + '</span>');
            dialog.dom.find('.cancel').bind('click',callbackcancel);
        };
        callbackok || callbackcancel ? dialog.dom.find('.openDialog_buttons').show() : dialog.dom.find('.openDialog_footer').hide();
        dialog.dom.find('.openDialog_buttons .button').css({backgroundColor:dialog.config.globalBackground,color:dialog.config.globalColor,borderRadius:dialog.config.globalRadius,border:dialog.config.globalBorder});
        if(dialog.config.autoOpen)
            dialog.dom.fadeIn();
        return dialog;
    }
 
})(jQuery); 